home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / Activex Documents / Resource & QD Globals notes < prev   
Text File  |  1996-09-04  |  3KB  |  120 lines

  1. //    Resources in the shared library world are tricky.
  2. //    If you want to use any of the resources in your shared library then you need
  3. //    to reset the current resource file to your shared library, then return it to the
  4. //    initial condition when you finish. The easy way to do this is with a C++ object
  5. //    which sets up your resource file on creation and restores it on destruction.
  6.  
  7. #include "DllEntry.h"
  8.  
  9. class CCFragResource
  10. {
  11.     public:
  12.         CCFragResource();
  13.         ~CCFragResource();
  14.  
  15.     private:
  16.         short    InResRefNum;
  17. };
  18.  
  19.  
  20. CCFragResource::CCFragResource()
  21. {
  22.     short ResRefNum = ::GetDllResRefNum();
  23.     InResRefNum = ::CurResFile();
  24.     if (ResRefNum != -1)
  25.         ::UseResFile(ResRefNum);
  26. }
  27.  
  28.  
  29. CCFragResource::~CCFragResource()
  30. {
  31.     if (InResRefNum != -1)
  32.         ::UseResFile(InResRefNum);
  33. }
  34.  
  35. //    in your code, when you want to use resources
  36.     {
  37.         CFFragResource    ResourceSetter;
  38.         PicHandle        PictH;
  39.  
  40.         PictH = GetResource('PICT',128);
  41.  
  42.         //    ResourceSetter goes out of scope and restores the resource chain
  43.     }
  44.  
  45.  
  46. //    Getting the containing apps globals is done something like this:
  47.  
  48.  
  49. QDGlobalsPtr GetQDGlobals(void)
  50. {
  51.     ProcessSerialNumber PSN;
  52.     FSSpec                myFSSpec;
  53.     Str63                name;
  54.     ProcessInfoRec        infoRec;
  55.     OSErr                Result = noErr;
  56.     CFragConnectionID    connID;
  57.     Str255                 errName;
  58.     QDGlobalsPtr        QDGlobals = NULL;
  59.     //
  60.     // Ask the system if CFM is available.
  61.     //
  62.     long response;
  63.     OSErr err = ::Gestalt(gestaltCFMAttr, &response);
  64.     Boolean hasCFM = ::BitTst(&response, 31-gestaltCFMPresent);
  65.             
  66.     if (hasCFM)
  67.     {
  68.         //
  69.         // GetProcessInformation takes a process serial number and 
  70.         // will give us back the name and FSSpec of the application.
  71.         // See the Process Manager in IM.
  72.         //
  73.         infoRec.processInfoLength = sizeof(ProcessInfoRec);
  74.         infoRec.processName = name;
  75.         infoRec.processAppSpec = &myFSSpec;
  76.         
  77.         PSN.highLongOfPSN = 0;
  78.         PSN.lowLongOfPSN = kCurrentProcess;
  79.         
  80.         Result = ::GetProcessInformation(&PSN, &infoRec);
  81.     }
  82.     else
  83.         //
  84.         // If no CFM installed, assume it must be a 68K app.
  85.         //
  86.         Result = -1;        
  87.         
  88.     if (Result == noErr)
  89.     {
  90.         //
  91.         // Now that we know the app name and FSSpec, we can call GetDiskFragment
  92.         // to get a connID to use in a subsequent call to FindSymbol (it will also
  93.         // return the address of “main” in app, which we ignore).  If GetDiskFragment 
  94.         // returns an error, we assume the app must be 68K.
  95.         //
  96.         Ptr mainAddr;     
  97.         Result =  ::GetDiskFragment(infoRec.processAppSpec, 0L, 0L, infoRec.processName,
  98.                                   kLoadCFrag, &connID, (Ptr*)&mainAddr, errName);
  99.     }
  100.  
  101.     if (Result == noErr) 
  102.     {
  103.         //
  104.         // The app is a PPC code fragment, so call FindSymbol
  105.         // to get the exported “qd” symbol so we can access its
  106.         // QuickDraw globals.
  107.         //
  108.         CFragSymbolClass    SymClass;
  109.         Result = ::FindSymbol(connID, "\pqd", (Ptr*)&QDGlobals, &SymClass);
  110.     }
  111.     else
  112.         //
  113.         // The app is 68K, so use its A5 to compute the address
  114.         // of its QuickDraw globals.
  115.         //
  116.         QDGlobals = QDGlobalsPtr(*((long*)SetCurrentA5()) - (sizeof(QDGlobals) - sizeof(GrafPtr)));
  117.  
  118.     return QDGlobals;
  119. }
  120.